//引入vue和vue-router，并赋值给相应的vue和Router
import Vue from 'vue'
import Router from 'vue-router'
import VueResource from 'vue-resource'
//引入路由
import First from '@/components/First.vue'
import Search from '@/components/Search.vue'
import Login from '@/components/Login.vue'
import Reg from '@/components/Reg.vue'
import Community from '@/components/Community.vue'
import Mine from '@/components/Mine.vue'
import Body from '@/components/Body.vue'
import Myworks from '@/components/Myworks.vue'
import Subw from '@/components/Subw.vue'
import Error from '@/components/Error.vue'
import HelloWorld from '@/components/HelloWorld.vue'
import Child from '@/components/Child.vue'
import CitChild from '@/components/CitChild.vue'
import Detail from '@/components/Detail.vue'
import Forget from '@/components/Forget.vue'
import Subimg from '@/components/Subimg.vue'
import Attention from '@/components/Attention.vue'
import Author from '@/components/Author.vue'
import Auchild from '@/components/Auchild.vue'
import Index from '@/components/Index.vue'

const originalPush = Router.prototype.push				//一下四行避免了社区界面的导航的冗余
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
const router = new Router()

router.beforeEach((to, from, next) => {
	console.log(to)
  if(to.path.startsWith('/')){
	  window.localStorage.removeItem('data-mine')
	  next()
  }else{
	  let mine=JSON.parse(window.localStorage.getItem('data-mine'))
	  //验证是否登录，若未登录跳转登录界面，否则验证token
	  if(!mine){
		  next({path:'/'})
	  }else{
		  //校验token合法性
		  axios({
			  url:'http://localhost:8081/user/checkToken',
			  method:'post',
			  headers:{
				  token:mine.token
			  }
		  }).then((response)=>{
			  console.log(response.data)
			  if(!response.data){
				  console.log('校验失败')
				  next({path:'/error'})
			  }
		  })
		  next()			//按照路由继续执行
	  }
  }
  
});


export default new Router({
  routes: [
    {	
		path:'/',
		name:'Login',
		component:Login
	},{
		path:'/Search',
		name:'Search',
		component:Search
	},{
		path:'/First',
		name:'First',
		component:First
	},{
		path:'/Reg',
		name:'Reg',
		component:Reg
	},{
		path:'/Community',
		name:'Community',
		component:Community,
		children:[
			{
				path:'/CitChild',
				name:'CitChild',
				component:CitChild
			}
		]
	},{
		path:'/Mine',
		name:'Mine',
		component:Mine
	},{
		path:'/',
		name:'Body',
		component:Body
	},{
		path:'/Subw',
		name:'Subw',
		component:Subw
	},{
		path:'/Myworks',
		name:'Myworks',
		component:Myworks,
		children:[
			{
				path:'/Child',
				name:'Child',
				component:Child
			}
		]
	},{
		path:'/Error',
		name:'Error',
		component:Error
	},{
		path:'/Detail',
		name:'Detail',
		component:Detail
	},{
		path:'/Forget',
		name:'Forget',
		component:Forget
	},{
		path:'/Subimg',
		name:'Subimg',
		component:Subimg
	},{
		path:'/Attention',
		name:'Attention',
		component:Attention
	},{
		path:'/Author',
		name:'Author',
		component:Author,
		children:[
			{
				path:'/Auchild',
				name:'Auchild',
				component:Auchild
			}
		]
	},{
		path:'/Index',
		name:'Index',
		component:Index
	}
  ],
  mode:"history"
});


